package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IAdvancedTaskModel;
import com.wgts.wgts.mvp.model.IShopKeepFragmentModel;
import com.wgts.wgts.mvp.presenter.IShopKeepFragmentPersenter;
import com.wgts.wgts.mvp.view.IShopKeepFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.AppModleRequest;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.netmodle.resp.ShopKeepFansEntity;
import com.wgts.wgts.netmodle.resp.ShopKeepTaskEntity;
import com.wgts.wgts.netmodle.resp.ShopKeeperEntity;

import java.util.ArrayList;
import java.util.List;

public class ShopKeepFragmentPersenter implements IShopKeepFragmentPersenter {

    private IShopKeepFragmentModel model;

    private IShopKeepFragmentView view;

    private IAdvancedTaskModel advancedTaskModel;

    public ShopKeepFragmentPersenter(IShopKeepFragmentModel model, IShopKeepFragmentView view, IAdvancedTaskModel advancedTaskModel) {
        this.model = model;
        this.view = view;
        this.advancedTaskModel = advancedTaskModel;
    }

    @Override
    public void initData() {
        model.initData(new HttpResultSubscriber<ShopKeeperEntity>() {
            @Override
            public void onSuccess(ShopKeeperEntity entity) {
                view.initResult(entity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void initTask() {
        advancedTaskModel.shopKeepTask(new HttpResultSubscriber<List<ShopKeepTaskEntity>>() {
            @Override
            public void onSuccess(List<ShopKeepTaskEntity> shopKeepTaskEntities) {
                ArrayList<ShopKeepTaskEntity> result = new ArrayList<>();
               for(ShopKeepTaskEntity entity : shopKeepTaskEntities){
                   if(entity.taskStatus.equals("1")){
                       result.add(entity);
                   }
               }

               if(result.size() > 3){
                   view.initTaskResult(result.subList(0, 3));
                   return;
               }

               view.initTaskResult(result);

            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void initMohu() {
        AppModleRequest request = new AppModleRequest();
        request.parentTypeId = -1;
        request.scene = "6";
        model.initMoHu(request, new HttpResultSubscriber<List<AppModleEntity>>() {
            @Override
            public void onSuccess(List<AppModleEntity> appModleEntities) {
                view.initMohuModel(appModleEntities);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

}
